package crud;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;

public class Dao<T> {
	
	private String classEmployeeId = "1006";
	private static HashMap<String, Employee> employeeMap = new HashMap<String, Employee>();
	private T t;
	
	static{
		employeeMap.put("1001", new Employee("1001","sun","jifeng","sjf@outlook.com"));
		employeeMap.put("1002", new Employee("1002","li","zhengyu","lzy@outlook.com"));
		employeeMap.put("1003", new Employee("1003","cui","lingjie","clj@outlook.com"));
		employeeMap.put("1004", new Employee("1004","liu","zihao","lzh@outlook.com"));
		employeeMap.put("1005", new Employee("1005","zhang","pengbo","zpb@outlook.com"));
	}
	public Dao(T t) { 
		this.t = t;
	}
	public ArrayList<String> getBeanColumnNames(){
		ArrayList<String> columnNames = new ArrayList<String>();
		Field[] fields=t.getClass().getDeclaredFields();
        for(int i=0; i<fields.length; i++){
        	columnNames.add(fields[i].getName());
        }
        return columnNames;
	}
	public void creatEmployee(Employee employee){
		employee.setId(classEmployeeId);
		employeeMap.put(classEmployeeId, employee);
		Integer id = new Integer(classEmployeeId);
		classEmployeeId = String.valueOf(id++);
	}
	public ArrayList<Employee> readAllEmployee(){
		return new ArrayList<Employee>(employeeMap.values());
	}
	public void updateEmployee(Employee employee){
		employeeMap.put(employee.getId(), employee);
	}
	public void deleteEmployee(String employeeId){
		employeeMap.remove(employeeId);
	}
	public Employee getEmployee(String id){
		return employeeMap.get(id);
	}
}
